package cn.uppower.wordaliy.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;

import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.model.FavoriteWords;
import cn.uppower.wordaliy.model.PageWord;
import cn.uppower.wordaliy.model.Words;
import cn.uppower.wordaliy.widget.BaseActivity;

public class CnEngChoiceActivity extends BaseActivity {


    private List<PageWord> pageWordList;
    private int trueWordIndex;
    private TextView tv_next_words,tv_words_prompt,tv_words_desc1,tv_words_desc2,tv_words_desc3,tv_words_desc4,tv_words_symbol,tv_words,tv_have_study,tv_words_prompt_text;
    private ImageView iv_favorite,iv_word_image_boolean1,iv_word_image_boolean2,iv_word_image_boolean3,iv_word_image_boolean4,iv_words_image_desc;
    
    private RelativeLayout rl_option1,rl_option2,rl_option3,rl_option4;

    private int alreadyStudied=0;
    private PageWord nowPageWord;
    private boolean isFavorite=false;
    private int type = 0;
    private int errorRepeatedWords= 0;
    private Set<Integer> errorWords =new LinkedHashSet<>();



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();

    }

    private void initData() {
//        wordsList = LitePal.findAll(Words.class);
        errorRepeatedWords=WordaliyApplication.wordaliySharedPreferences.getInt("NumberOfRepeatedWords",2);
        pageWordList= (List<PageWord>) getIntent().getSerializableExtra("words");
        Log.e("tyc",pageWordList.toString());
        type=getIntent().getIntExtra("type",1);
        nowPageWord=pageWordList.get(alreadyStudied);
        updateDatainfo(nowPageWord);

    }

    private void updateDatainfo(PageWord nowPageWord) {
        reSetAll();
        //        设置提示隐藏
        tv_words_prompt_text.setVisibility(View.GONE);
//        设置下一个单词不是收藏的
        isFavorite=false;
        iv_favorite.setImageResource(R.mipmap.favorite_no);
//        隐藏下一个单词按钮，知道答案正确再显示
        tv_next_words.setVisibility(View.GONE);
//        设置需要新学的单词数，等于总单词数减去当前已学个数
        tv_have_study.setText("需新学 "+(pageWordList.size()-alreadyStudied));

        if (type==1){
            tv_words.setText(nowPageWord.getCorrectWords().getEnglish());
            tv_words_symbol.setText("/"+nowPageWord.getCorrectWords().getSymbol()+"/");

//        设置图片描述
            iv_words_image_desc.setImageResource(nowPageWord.getCorrectWords().getImage());
        }else if (type==2){
            tv_words.setText(nowPageWord.getCorrectWords().getChinese());
            tv_words_symbol.setVisibility(View.GONE);
            iv_words_image_desc.setVisibility(View.GONE);
        }
        if (nowPageWord.isFavorite()){
            iv_favorite.setImageResource(R.mipmap.favorite_yes);
            isFavorite=true;
        }else {
            iv_favorite.setImageResource(R.mipmap.favorite_no);
            isFavorite=false;
        }
        setWordsOptions(nowPageWord);
    }


    private void setWordsOptions(PageWord nowPageWord) {
        Random random= new Random();
        trueWordIndex=random.nextInt(4)+1;
        if (type==1){
            switch (trueWordIndex){
                case 1:
                    tv_words_desc1.setText(nowPageWord.getCorrectWords().getChinese());
                    setOneOther(nowPageWord);
                    break;
                case 2:
                    tv_words_desc2.setText(nowPageWord.getCorrectWords().getChinese());
                    setTwoOther(nowPageWord);
                    break;
                case 3:
                    tv_words_desc3.setText(nowPageWord.getCorrectWords().getChinese());
                    setThreeOther(nowPageWord);
                    break;
                case 4:
                    tv_words_desc4.setText(nowPageWord.getCorrectWords().getChinese());
                    setFourOther(nowPageWord);
                    break;
            }
        }else if (type==2){
            switch (trueWordIndex){
                case 1:
                    tv_words_desc1.setText(nowPageWord.getCorrectWords().getEnglish());
                    setOneOther(nowPageWord);
                    break;
                case 2:
                    tv_words_desc2.setText(nowPageWord.getCorrectWords().getEnglish());
                    setTwoOther(nowPageWord);
                    break;
                case 3:
                    tv_words_desc3.setText(nowPageWord.getCorrectWords().getEnglish());
                    setThreeOther(nowPageWord);
                    break;
                case 4:
                    tv_words_desc4.setText(nowPageWord.getCorrectWords().getEnglish());
                    setFourOther(nowPageWord);
                    break;
            }
        }
    }
    private void setOneOther(PageWord nowPageWord){
        tv_words_desc2.setText(nowPageWord.getErrorOption1());
        tv_words_desc3.setText(nowPageWord.getErrorOption2());
        tv_words_desc4.setText(nowPageWord.getErrorOption3());
    }

    private void setTwoOther(PageWord nowPageWord){
        tv_words_desc1.setText(nowPageWord.getErrorOption1());
        tv_words_desc3.setText(nowPageWord.getErrorOption2());
        tv_words_desc4.setText(nowPageWord.getErrorOption3());

    }

    private void setThreeOther(PageWord nowPageWord){
        tv_words_desc1.setText(nowPageWord.getErrorOption1());
        tv_words_desc2.setText(nowPageWord.getErrorOption2());
        tv_words_desc4.setText(nowPageWord.getErrorOption3());

    }
    private void setFourOther(PageWord nowPageWord){
        tv_words_desc1.setText(nowPageWord.getErrorOption1());
        tv_words_desc2.setText(nowPageWord.getErrorOption2());
        tv_words_desc3.setText(nowPageWord.getErrorOption3());

    }

    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_cn_eng_choice;
    }

    @Override
    public void init() {
        tv_next_words = findViewById(R.id.tv_next_words);
        iv_favorite = findViewById(R.id.iv_favorite);
        tv_words_prompt = findViewById(R.id.tv_words_prompt);
        rl_option1 = findViewById(R.id.rl_option1);
        rl_option2 = findViewById(R.id.rl_option2);
        rl_option3 = findViewById(R.id.rl_option3);
        rl_option4 = findViewById(R.id.rl_option4);
        iv_word_image_boolean1 = findViewById(R.id.iv_word_image_boolean1);
        iv_word_image_boolean2 = findViewById(R.id.iv_word_image_boolean2);
        iv_word_image_boolean3 = findViewById(R.id.iv_word_image_boolean3);
        iv_word_image_boolean4 = findViewById(R.id.iv_word_image_boolean4);
        iv_words_image_desc = findViewById(R.id.iv_words_image_desc);


        tv_words_desc1 = findViewById(R.id.tv_words_desc1);
        tv_words_desc2 = findViewById(R.id.tv_words_desc2);
        tv_words_desc3 = findViewById(R.id.tv_words_desc3);
        tv_words_desc4 = findViewById(R.id.tv_words_desc4);
        tv_words = findViewById(R.id.tv_words);
        tv_words_symbol = findViewById(R.id.tv_words_symbol);
        tv_have_study = findViewById(R.id.tv_have_study);
        tv_words_prompt_text = findViewById(R.id.tv_words_prompt_text);
        tv_words_prompt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPrompt();
            }
        });

        iv_favorite.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFavorite){
                    iv_favorite.setImageResource(R.mipmap.favorite_no);
                    LitePal.deleteAll(FavoriteWords.class,"wordsId = ? and userPhone = ?",String.valueOf(pageWordList.get(alreadyStudied).getCorrectWords().getId()),WordaliyApplication.userInfo.getUserPhone());
                    isFavorite=false;
                }else {
                    iv_favorite.setImageResource(R.mipmap.favorite_yes);
                    new FavoriteWords(pageWordList.get(alreadyStudied).getCorrectWords().getId(),WordaliyApplication.userInfo.getUserPhone()).save();
                    isFavorite=true;
                }
            }
        });
        rl_option1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                setAllOptionDisable();
                if (trueWordIndex==1){
//                    iv_word_image_boolean1.setImageResource(R.mipmap.word_ok);
//                    tv_next_words.setVisibility(View.VISIBLE);
                    setNextWordIsClose();
                    nextWords();

                }else {
                    iv_word_image_boolean1.setImageResource(R.mipmap.word_error);
//                    tv_next_words.setVisibility(View.GONE);
                    tv_next_words.setVisibility(View.VISIBLE);
                    errorWords.add(alreadyStudied);
                    iv_word_image_boolean1.setVisibility(View.VISIBLE);
                    showPrompt();
                }

            }
        });


        rl_option2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                setAllOptionDisable();
                if (trueWordIndex==2){
//                    iv_word_image_boolean2.setImageResource(R.mipmap.word_ok);
//                    tv_next_words.setVisibility(View.VISIBLE);
                    setNextWordIsClose();
                    nextWords();

                }else {
                    iv_word_image_boolean2.setImageResource(R.mipmap.word_error);
                    //                    tv_next_words.setVisibility(View.GONE);
                    tv_next_words.setVisibility(View.VISIBLE);
                    errorWords.add(alreadyStudied);
                    iv_word_image_boolean2.setVisibility(View.VISIBLE);
                    showPrompt();
                }

            }
        });

        rl_option3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                setAllOptionDisable();
                if (trueWordIndex==3){
//                    iv_word_image_boolean3.setImageResource(R.mipmap.word_ok);
//                    tv_next_words.setVisibility(View.VISIBLE);
                    setNextWordIsClose();
                    nextWords();

                }else {
                    iv_word_image_boolean3.setImageResource(R.mipmap.word_error);
                    //                    tv_next_words.setVisibility(View.GONE);
                    tv_next_words.setVisibility(View.VISIBLE);
                    errorWords.add(alreadyStudied);
                    iv_word_image_boolean3.setVisibility(View.VISIBLE);
                    showPrompt();

                }
            }
        });

        rl_option4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                setAllOptionDisable();
                if (trueWordIndex==4){
//                    iv_word_image_boolean4.setImageResource(R.mipmap.word_ok);
//                    tv_next_words.setVisibility(View.VISIBLE);
                    setNextWordIsClose();
                    nextWords();

                }else {
                    iv_word_image_boolean4.setImageResource(R.mipmap.word_error);
                    //                    tv_next_words.setVisibility(View.GONE);
                    tv_next_words.setVisibility(View.VISIBLE);
                    errorWords.add(alreadyStudied);
                    iv_word_image_boolean4.setVisibility(View.VISIBLE);
                    showPrompt();
                }

            }
        });
        tv_next_words.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               nextWords();
            }
        });
    }

    private void showPrompt() {
        tv_words_prompt_text.setText("提示："+nowPageWord.getCorrectWords().getPrompt());
        tv_words_prompt_text.setVisibility(View.VISIBLE);
    }

    private void nextWords() {
        int res= checkErrorWords();
        if (res==-1){
            nowPageWord=pageWordList.get(alreadyStudied);
            updateDatainfo(nowPageWord);
        }else{
            PageWord pageWord=pageWordList.get(res);
            updateDatainfo(pageWord);
        }
    }

    private int checkErrorWords() {
        for (int i:errorWords){
            if (alreadyStudied==i+errorRepeatedWords){
                errorWords.remove(i);
                return i;
            }
        }
        alreadyStudied++;
        return -1;

    }

    private void reSetAll(){
        iv_word_image_boolean1.setVisibility(View.GONE);
        iv_word_image_boolean2.setVisibility(View.GONE);
        iv_word_image_boolean3.setVisibility(View.GONE);
        iv_word_image_boolean4.setVisibility(View.GONE);
        setAllOptionEnable();
    }

    private void setAllOptionDisable(){
        rl_option1.setEnabled(false);
        rl_option2.setEnabled(false);
        rl_option3.setEnabled(false);
        rl_option4.setEnabled(false);
    }

    private void setAllOptionEnable(){
        rl_option1.setEnabled(true);
        rl_option2.setEnabled(true);
        rl_option3.setEnabled(true);
        rl_option4.setEnabled(true);
    }
    private void setNextWordIsClose(){
//        说明还不是最后一个单词
        if (alreadyStudied!=pageWordList.size()-1){
            return;
        }
//学习完成，点击返回到选择页面
        tv_next_words.setText("完成");
        tv_next_words.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}
